/**
 * @Author: zhongyang
 * @Date: 2022/5/30 15:38
 */

package main

import (
	"bytes"
	"fmt"
	"github.com/muesli/cache2go"
	"mine-info-server/core"
	"mine-info-server/global"
	"mine-info-server/initialize"
	"os"
	"os/exec"
)

// @title 哥谭
// @version 1.0
func main() {
	runCommand()
	global.GVA_VP = core.Viper()      // 初始化Viper
	global.GVA_LOG = core.Zap()       // 初始化zap日志库
	global.GVA_DB = initialize.Gorm() // gorm连接数据库
	global.GVA_CACHE = cache2go.Cache(global.GVA_CONFIG.LocalCache.Name)
	initialize.DBList()
	if global.GVA_DB != nil {
		initialize.RegisterTables(global.GVA_DB) // 初始化表
		//initialize.RegisterBaseData(global.GVA_DB) // 填充基础数据（如果存在则不会填充）
		// 程序结束前关闭数据库链接
		db, _ := global.GVA_DB.DB()
		defer db.Close()
	}
	initialize.Timer()
	core.RunWindowsServer()
}

// 执行之前先跑swagger文档
func runCommand() {
	cmd := exec.Command("swag", "init")
	fmt.Println("Cmd", cmd.Args)
	var out bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = os.Stderr
	err := cmd.Start()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(out.String())
}
